import { doorStore } from '@/store/door'

import Request from './luch-request/index.js'

const BASE_API = {
  VITE_APP_API_BASEURL: 'https://ddsdk.doordu.com',
  VITE_APP_CAS_BASE_URL: 'https://static-test.jinzhuangli.com',
  VITE_APP_CAS_STATIC_BASE_URL: 'https://static-test.jinzhuangli.com/staticFile/images/',
}

const http = new Request({
  baseURL: import.meta.env.MODE === 'development' ? '' : BASE_API.VITE_APP_API_BASEURL, // 设置请求的url
  timeout: 30000, // 设置延迟时间
})
// 请求拦截器
http.interceptors.request.use(
  (config: any) => {
    // 可使用async await 做异步操作
    // const token = getStorage(TokenKey);
    const useDoorStore = doorStore()
    config.header = {
      ...config.header,
      Token: useDoorStore.getToken(),
    }

    return config
  },
  (error) => {
    return Promise.resolve(error)
  },
)

// 响应拦截器
http.interceptors.response.use(
  (response: any) => {
    if (response.data.message == '请重新登录' || response.data.code == 401) {
      console.error(response.data)
    }
    return response.data
  },
  (error) => {
    console.log(error, 'error')

    return Promise.resolve(error)
  },
)
export { http }
